package com.unytech.project.web.interceptor;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.unytech.project.annotation.ActionMethodLabel;
import com.unytech.project.common.Globals;
import com.unytech.project.entity.security.ErrorRecord;
import com.unytech.project.entity.security.OperateRecord;
import com.unytech.project.entity.system.Permission;
import com.unytech.project.service.security.ErrorRecordService;
import com.unytech.project.utils.StringUtil;

public class SystemExceptionInterceptor extends AbstractInterceptor {
	final private Log logger = LogFactory.getLog(getClass());
	
//	@Resource private OperateRecordService operateRecordService;
	@Resource private ErrorRecordService errorRecordService;
	
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		logger.info("-----进入系统例外拦截器-----");
		ActionContext act = invocation.getInvocationContext();
		ActionProxy ap = invocation.getProxy();
		Method method = ap.getAction().getClass().getMethod(ap.getMethod(), null);
		ActionMethodLabel abl = null;
		if (method != null) {
			abl = method.getAnnotation(ActionMethodLabel.class);
		}
					
		StringBuilder message = new StringBuilder();
		message.append(invocation.getProxy().getNamespace()).append("/").append(invocation.getProxy().getActionName());
		
		Date startdate = new Date();
		long startTime = System.currentTimeMillis();
		String result = null;
		long endTime = 0;
		long executionTime = 0;
		try {
			result = invocation.invoke();
			endTime = System.currentTimeMillis();
			executionTime = endTime - startTime;
		} catch (Exception e) {
			logger.info(e.getMessage());
			logger.error(e, e);
			
			//实例化操作信息
			OperateRecord operaterecord = new OperateRecord();
			operaterecord.setRecorduri(message.toString());
			
			operaterecord.setRecordmodule(StringUtil.analyRUI((List<Permission>) act.getApplication().get(Globals.CONTEXT_ALLPERMISSIONS_KEY), message.toString()));
			if (abl !=null) {
				operaterecord.setOperateaction(abl.methodnote());
			}
			operaterecord.setStarttime(startdate);
		    operaterecord.setEndtime(new Date());
		    operaterecord.setElapsedtime((int) executionTime);
		    
		    //实例化例外信息
		    ErrorRecord errorrecord = null;
		    errorrecord = (ErrorRecord) act.getSession().get(Globals.SESSION_ERRORRECORD);
		    if (errorrecord == null) {
		    	errorrecord = new ErrorRecord();
		    	errorrecord.setErrordescribe("系统拦截器：未知错误");
		    }
		    
			errorrecord.setErrorclass(message.toString());
			errorrecord.setErrorcontext(e.getMessage());
			errorrecord.setOperaterecord(operaterecord);
			
		    //把例外信息写入SESSION，提供给前台显示
		    invocation.getInvocationContext().getSession().put(Globals.SESSION_OPERATERECORD_KEY, operaterecord);
		    
		    //把例外信息写入数据库
		    //这以层的错误不知道怎么处理，再者拦截器抛出的错误不知道struts2是怎么处理的
		    try {
		    	errorRecordService.save(errorrecord);
		    } catch (Exception ee) {
		    	logger.info(ee.getMessage());
				logger.error(ee, ee);
		    	throw ee;
		    } 
			return Action.ERROR;
		}	
		return result;
	}

	
	
}
